package com.aabbc.webflux.main;

import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.server.EntityResponse;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;

/**
 * Main
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-06-01 11:24
 * @blame Team
 */
public class Main {

    public static void main(String[] args) {
//        Mono.just("Hello").subscribe(System.out::println);
//        Mono.fromSupplier(() -> "Hello").subscribe(System.out::println);
//        Mono.fromSupplier(() -> null).subscribe(System.out::println);
//        Mono.justOrEmpty(Optional.of("Hello")).subscribe(System.out::println);
//        Mono.justOrEmpty(Optional.empty()).subscribe(System.out::println);
//        Mono.create(sink -> sink.success("Hello")).subscribe(System.out::println);
//
//        System.out.println("===================================================");
//        Flux.just("Hello", "World").subscribe(System.out::println);
//        Flux.fromArray(new Integer[] {1, 2, 3}).subscribe(System.out::println);
//        Flux.empty().subscribe(System.out::println);
//        Flux.range(1, 10).subscribe(System.out::println);
//        Flux.interval(Duration.of(10, ChronoUnit.SECONDS)).subscribe(System.out::println);
//
//        System.out.println("===================================================");
//        Flux.generate(sink -> {
//            sink.next("Hello");
//            sink.complete();
//        }).subscribe(System.out::println);
//
//
//        final Random random = new Random();
//        Flux.generate(ArrayList::new, (list, sink) -> {
//            int value = random.nextInt(100);
//            list.add(value);
//            sink.next(value);
//            if (list.size() == 10) {
//                sink.complete();
//            }
//            return list;
//        }).subscribe(System.out::println);
//
//        System.out.println("===================================================");
//        Flux.range(1, 1000).take(10).subscribe(System.out::println);
//        Flux.range(1, 1000).takeLast(10).subscribe(System.out::println);
//        Flux.range(1, 1000).takeWhile(i -> i <= 10).subscribe(System.out::println);
//        Flux.range(1, 1000).takeUntil(i -> i == 10).subscribe(System.out::println);
//
//        System.out.println("===================================================");
//        Flux.just("a", "b")
//                .zipWith(Flux.just("c", "d"))
//                .subscribe(System.out::println);
//        Flux.just("a", "b")
//                .zipWith(Flux.just("c", "d"), (s1, s2) -> String.format("%s-%s", s1, s2))
//                .subscribe(System.out::println);
//
//        System.out.println("===================================================");
//        Flux.range(1, 10).reduce(Integer::sum).subscribe(System.out::println);
//        Flux.range(1, 10).reduceWith(() -> 10, Integer::sum).subscribe(System.out::println);
//
//        System.out.println("===================================================");
//        Flux.just(1, 2)
//                .concatWith(Mono.error(new IllegalStateException()))
//                .subscribe(System.out::println, System.err::println);
//        Flux.just(1, 2)
//                .concatWith(Mono.error(new IllegalStateException()))
//                .onErrorReturn(0)
//                .subscribe(System.out::println);


        System.out.println("===================================================");
//        System.out.println(Mono.justOrEmpty(java.util.Optional.empty()).block());
//        System.out.println(Mono.justOrEmpty(java.util.Optional.empty()).defaultIfEmpty("abc").block());
//        System.out.println(Mono.justOrEmpty(java.util.Optional.of("a")).defaultIfEmpty("abc").block());
//        System.out.println(Mono.justOrEmpty(java.util.Optional.empty()).switchIfEmpty(Mono.error(new RuntimeException("null"))).block());

//        Flux<String> emptyFlux = Mono.<String>justOrEmpty(java.util.Optional.empty()).flux();
//        System.out.println(emptyFlux.collectList().block());
//        System.out.println(emptyFlux.collectList().defaultIfEmpty("").block());
//        System.out.println(Flux.justOrEmpty(java.util.Optional.of("a")).defaultIfEmpty("abc").block());
//        System.out.println(Flux.justOrEmpty(java.util.Optional.empty()).switchIfEmpty(Mono.error(new RuntimeException("null"))).block());


        Mono<EntityResponse<String>> response = EntityResponse.fromObject("OK").build();
        System.out.println(Objects.requireNonNull(response.block()).statusCode().value());
        System.out.println(response.switchIfEmpty(Mono.error(new IllegalArgumentException())).block().entity());

        Mono<ServerResponse> server = ServerResponse.ok().syncBody("OK");
        System.out.println(server.blockOptional().get().statusCode().value());


        System.out.println("done.");
    }
}
